<?php
    abstract class FormProcessor
    {
        protected $_errors = array();
        protected $_vals = array();
        private $_sanitizeChain = null;

        public function __construct()
        {

        }

        abstract function process(Zend_Controller_Request_Abstract $request);

        public function sanitize($value)
        {
            if (!$this->_sanitizeChain instanceof Zend_Filter) {
                $this->_sanitizeChain = new Zend_Filter();
                $this->_sanitizeChain->addFilter(new Zend_Filter_StringTrim())
                                     ->addFilter(new Zend_Filter_StripTags());
            }

            // filter out any line feeds / carriage returns
            $ret = preg_replace('/[\r\n]+/', ' ', $value);

            // filter using the above chain
            return $this->_sanitizeChain->filter($ret);
        }

        public function addError($key, $val)
        {
            if (array_key_exists($key, $this->_errors)) {
                if (!is_array($this->_errors[$key]))
                    $this->_errors[$key] = array($this->_errors[$key]);

                $this->_errors[$key][] = $val;
            }
            else
                $this->_errors[$key] = $val;
        }

        public function getError($key)
        {
            if ($this->hasError($key))
                return $this->_errors[$key];

            return null;
        }

        public function getErrors()
        {
            return $this->_errors;
        }

        public function hasError($key = null)
        {
            if (strlen($key) == 0)
                return count($this->_errors) > 0;

            return array_key_exists($key, $this->_errors);
        }

        public function __set($name, $value)
        {
            $this->_vals[$name] = $value;
        }

        public function __get($name)
        {
            return array_key_exists($name, $this->_vals) ? $this->_vals[$name] : null;
        }
        
          public function rewrite($title)
        {
          /*   // составляем URL для запроса к API key-affinity-529
               $url = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20140324T121547Z.b6cdf5836c7bc0c4.c859131db40910165d19e9709e08f643dc992417&text=' . urlencode($title) . '&lang=en';

               // обращаемся к API и декодируем результат
               $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_REFERER, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                 $translate = curl_exec($ch);
                curl_close($ch);
               $json = json_decode($translate, true);
              
             
               // если ответ не успешный, то возвращаем false
               if ($json['code'] != 200)
                   return false;
             
               $result = $json['text']; // получаем перевод
               $result = htmlspecialchars_decode($result[0]); // декодируем строку после перевода
               $result = stripslashes($result); // удаляем слэши
               $result = preg_replace('~\W~', '-', $result); // заменяем все специфические символы на минусы
               $result = preg_replace('~-+~', '-', $result); // удаляем лишние минусы
               $result = trim($result, '-'); // удаляем пробелы и минусы по бокам

               return strtolower($result);*/
                $str = trim($title);

               // $str = preg_replace("/[^а-яА-Яa-z0-9 \-]/i","",$str);

                $rus = array ("А","Б","В","Г","Д","Е","Ё","Ж","З","И","Й","К","Л",
                "М","Н","О","П","Р","С","Т","У","Ф","Х","Ц","Ч","Ш",
                "Щ","Ь","Ы","Ъ","Э","Ю","Я",
                "а","б","в","г","д","е","ё","ж","з","и","й","к","л",
                "м","н","о","п","р","с","т","у","ф","х","ц","ч","ш",
                "щ","ь","ы","ъ","э","ю","я"," ");

                $eng = array ("A","B","V","G","D","E","E","ZH","Z","I","Y","K","L",
                "M","N","O","P","R","S","T","U","F","H","C","CH","SH",
                "SCH","","Y","","E","YU","YA",
                "a","b","v","g","d","e","e","zh","z","i","y","k","l",
                "m","n","o","p","r","s","t","u","f","h","c","ch","sh",
                "sch","","y","","e","yu","ya","-");

                $res = str_replace($rus,$eng,$str);

                $res = str_replace(array("---","--"),"-",$res);
                $res = strtolower($res);

                return $res;
           
        }
    }
?>